<!DOCTYPE html><html><head>
      <title>Nginx 安装与多端口配置指南</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      
      <link rel="stylesheet" href="file:////Users/jianjian/.vscode/extensions/shd101wyy.markdown-preview-enhanced-0.8.18/crossnote/dependencies/katex/katex.min.css">
      
      
      
      
      
      <style>
      code[class*=language-],pre[class*=language-]{color:#333;background:0 0;font-family:Consolas,"Liberation Mono",Menlo,Courier,monospace;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.4;-moz-tab-size:8;-o-tab-size:8;tab-size:8;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none}pre[class*=language-]{padding:.8em;overflow:auto;border-radius:3px;background:#f5f5f5}:not(pre)>code[class*=language-]{padding:.1em;border-radius:.3em;white-space:normal;background:#f5f5f5}.token.blockquote,.token.comment{color:#969896}.token.cdata{color:#183691}.token.doctype,.token.macro.property,.token.punctuation,.token.variable{color:#333}.token.builtin,.token.important,.token.keyword,.token.operator,.token.rule{color:#a71d5d}.token.attr-value,.token.regex,.token.string,.token.url{color:#183691}.token.atrule,.token.boolean,.token.code,.token.command,.token.constant,.token.entity,.token.number,.token.property,.token.symbol{color:#0086b3}.token.prolog,.token.selector,.token.tag{color:#63a35c}.token.attr-name,.token.class,.token.class-name,.token.function,.token.id,.token.namespace,.token.pseudo-class,.token.pseudo-element,.token.url-reference .token.variable{color:#795da3}.token.entity{cursor:help}.token.title,.token.title .token.punctuation{font-weight:700;color:#1d3e81}.token.list{color:#ed6a43}.token.inserted{background-color:#eaffea;color:#55a532}.token.deleted{background-color:#ffecec;color:#bd2c00}.token.bold{font-weight:700}.token.italic{font-style:italic}.language-json .token.property{color:#183691}.language-markup .token.tag .token.punctuation{color:#333}.language-css .token.function,code.language-css{color:#0086b3}.language-yaml .token.atrule{color:#63a35c}code.language-yaml{color:#183691}.language-ruby .token.function{color:#333}.language-markdown .token.url{color:#795da3}.language-makefile .token.symbol{color:#795da3}.language-makefile .token.variable{color:#183691}.language-makefile .token.builtin{color:#0086b3}.language-bash .token.keyword{color:#0086b3}pre[data-line]{position:relative;padding:1em 0 1em 3em}pre[data-line] .line-highlight-wrapper{position:absolute;top:0;left:0;background-color:transparent;display:block;width:100%}pre[data-line] .line-highlight{position:absolute;left:0;right:0;padding:inherit 0;margin-top:1em;background:hsla(24,20%,50%,.08);background:linear-gradient(to right,hsla(24,20%,50%,.1) 70%,hsla(24,20%,50%,0));pointer-events:none;line-height:inherit;white-space:pre}pre[data-line] .line-highlight:before,pre[data-line] .line-highlight[data-end]:after{content:attr(data-start);position:absolute;top:.4em;left:.6em;min-width:1em;padding:0 .5em;background-color:hsla(24,20%,50%,.4);color:#f4f1ef;font:bold 65%/1.5 sans-serif;text-align:center;vertical-align:.3em;border-radius:999px;text-shadow:none;box-shadow:0 1px #fff}pre[data-line] .line-highlight[data-end]:after{content:attr(data-end);top:auto;bottom:.4em}html body{font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif;font-size:16px;line-height:1.6;color:#333;background-color:#fff;overflow:initial;box-sizing:border-box;word-wrap:break-word}html body>:first-child{margin-top:0}html body h1,html body h2,html body h3,html body h4,html body h5,html body h6{line-height:1.2;margin-top:1em;margin-bottom:16px;color:#000}html body h1{font-size:2.25em;font-weight:300;padding-bottom:.3em}html body h2{font-size:1.75em;font-weight:400;padding-bottom:.3em}html body h3{font-size:1.5em;font-weight:500}html body h4{font-size:1.25em;font-weight:600}html body h5{font-size:1.1em;font-weight:600}html body h6{font-size:1em;font-weight:600}html body h1,html body h2,html body h3,html body h4,html body h5{font-weight:600}html body h5{font-size:1em}html body h6{color:#5c5c5c}html body strong{color:#000}html body del{color:#5c5c5c}html body a:not([href]){color:inherit;text-decoration:none}html body a{color:#08c;text-decoration:none}html body a:hover{color:#00a3f5;text-decoration:none}html body img{max-width:100%}html body>p{margin-top:0;margin-bottom:16px;word-wrap:break-word}html body>ol,html body>ul{margin-bottom:16px}html body ol,html body ul{padding-left:2em}html body ol.no-list,html body ul.no-list{padding:0;list-style-type:none}html body ol ol,html body ol ul,html body ul ol,html body ul ul{margin-top:0;margin-bottom:0}html body li{margin-bottom:0}html body li.task-list-item{list-style:none}html body li>p{margin-top:0;margin-bottom:0}html body .task-list-item-checkbox{margin:0 .2em .25em -1.8em;vertical-align:middle}html body .task-list-item-checkbox:hover{cursor:pointer}html body blockquote{margin:16px 0;font-size:inherit;padding:0 15px;color:#5c5c5c;background-color:#f0f0f0;border-left:4px solid #d6d6d6}html body blockquote>:first-child{margin-top:0}html body blockquote>:last-child{margin-bottom:0}html body hr{height:4px;margin:32px 0;background-color:#d6d6d6;border:0 none}html body table{margin:10px 0 15px 0;border-collapse:collapse;border-spacing:0;display:block;width:100%;overflow:auto;word-break:normal;word-break:keep-all}html body table th{font-weight:700;color:#000}html body table td,html body table th{border:1px solid #d6d6d6;padding:6px 13px}html body dl{padding:0}html body dl dt{padding:0;margin-top:16px;font-size:1em;font-style:italic;font-weight:700}html body dl dd{padding:0 16px;margin-bottom:16px}html body code{font-family:Menlo,Monaco,Consolas,'Courier New',monospace;font-size:.85em;color:#000;background-color:#f0f0f0;border-radius:3px;padding:.2em 0}html body code::after,html body code::before{letter-spacing:-.2em;content:'\00a0'}html body pre>code{padding:0;margin:0;word-break:normal;white-space:pre;background:0 0;border:0}html body .highlight{margin-bottom:16px}html body .highlight pre,html body pre{padding:1em;overflow:auto;line-height:1.45;border:#d6d6d6;border-radius:3px}html body .highlight pre{margin-bottom:0;word-break:normal}html body pre code,html body pre tt{display:inline;max-width:initial;padding:0;margin:0;overflow:initial;line-height:inherit;word-wrap:normal;background-color:transparent;border:0}html body pre code:after,html body pre code:before,html body pre tt:after,html body pre tt:before{content:normal}html body blockquote,html body dl,html body ol,html body p,html body pre,html body ul{margin-top:0;margin-bottom:16px}html body kbd{color:#000;border:1px solid #d6d6d6;border-bottom:2px solid #c7c7c7;padding:2px 4px;background-color:#f0f0f0;border-radius:3px}@media print{html body{background-color:#fff}html body h1,html body h2,html body h3,html body h4,html body h5,html body h6{color:#000;page-break-after:avoid}html body blockquote{color:#5c5c5c}html body pre{page-break-inside:avoid}html body table{display:table}html body img{display:block;max-width:100%;max-height:100%}html body code,html body pre{word-wrap:break-word;white-space:pre}}.markdown-preview{width:100%;height:100%;box-sizing:border-box}.markdown-preview ul{list-style:disc}.markdown-preview ul ul{list-style:circle}.markdown-preview ul ul ul{list-style:square}.markdown-preview ol{list-style:decimal}.markdown-preview ol ol,.markdown-preview ul ol{list-style-type:lower-roman}.markdown-preview ol ol ol,.markdown-preview ol ul ol,.markdown-preview ul ol ol,.markdown-preview ul ul ol{list-style-type:lower-alpha}.markdown-preview .newpage,.markdown-preview .pagebreak{page-break-before:always}.markdown-preview pre.line-numbers{position:relative;padding-left:3.8em;counter-reset:linenumber}.markdown-preview pre.line-numbers>code{position:relative}.markdown-preview pre.line-numbers .line-numbers-rows{position:absolute;pointer-events:none;top:1em;font-size:100%;left:0;width:3em;letter-spacing:-1px;border-right:1px solid #999;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none}.markdown-preview pre.line-numbers .line-numbers-rows>span{pointer-events:none;display:block;counter-increment:linenumber}.markdown-preview pre.line-numbers .line-numbers-rows>span:before{content:counter(linenumber);color:#999;display:block;padding-right:.8em;text-align:right}.markdown-preview .mathjax-exps .MathJax_Display{text-align:center!important}.markdown-preview:not([data-for=preview]) .code-chunk .code-chunk-btn-group{display:none}.markdown-preview:not([data-for=preview]) .code-chunk .status{display:none}.markdown-preview:not([data-for=preview]) .code-chunk .output-div{margin-bottom:16px}.markdown-preview .md-toc{padding:0}.markdown-preview .md-toc .md-toc-link-wrapper .md-toc-link{display:inline;padding:.25rem 0}.markdown-preview .md-toc .md-toc-link-wrapper .md-toc-link div,.markdown-preview .md-toc .md-toc-link-wrapper .md-toc-link p{display:inline}.markdown-preview .md-toc .md-toc-link-wrapper.highlighted .md-toc-link{font-weight:800}.scrollbar-style::-webkit-scrollbar{width:8px}.scrollbar-style::-webkit-scrollbar-track{border-radius:10px;background-color:transparent}.scrollbar-style::-webkit-scrollbar-thumb{border-radius:5px;background-color:rgba(150,150,150,.66);border:4px solid rgba(150,150,150,.66);background-clip:content-box}html body[for=html-export]:not([data-presentation-mode]){position:relative;width:100%;height:100%;top:0;left:0;margin:0;padding:0;overflow:auto}html body[for=html-export]:not([data-presentation-mode]) .markdown-preview{position:relative;top:0;min-height:100vh}@media screen and (min-width:914px){html body[for=html-export]:not([data-presentation-mode]) .markdown-preview{padding:2em calc(50% - 457px + 2em)}}@media screen and (max-width:914px){html body[for=html-export]:not([data-presentation-mode]) .markdown-preview{padding:2em}}@media screen and (max-width:450px){html body[for=html-export]:not([data-presentation-mode]) .markdown-preview{font-size:14px!important;padding:1em}}@media print{html body[for=html-export]:not([data-presentation-mode]) #sidebar-toc-btn{display:none}}html body[for=html-export]:not([data-presentation-mode]) #sidebar-toc-btn{position:fixed;bottom:8px;left:8px;font-size:28px;cursor:pointer;color:inherit;z-index:99;width:32px;text-align:center;opacity:.4}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] #sidebar-toc-btn{opacity:1}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc{position:fixed;top:0;left:0;width:300px;height:100%;padding:32px 0 48px 0;font-size:14px;box-shadow:0 0 4px rgba(150,150,150,.33);box-sizing:border-box;overflow:auto;background-color:inherit}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc::-webkit-scrollbar{width:8px}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc::-webkit-scrollbar-track{border-radius:10px;background-color:transparent}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc::-webkit-scrollbar-thumb{border-radius:5px;background-color:rgba(150,150,150,.66);border:4px solid rgba(150,150,150,.66);background-clip:content-box}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc a{text-decoration:none}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc .md-toc{padding:0 16px}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc .md-toc .md-toc-link-wrapper .md-toc-link{display:inline;padding:.25rem 0}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc .md-toc .md-toc-link-wrapper .md-toc-link div,html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc .md-toc .md-toc-link-wrapper .md-toc-link p{display:inline}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc .md-toc .md-toc-link-wrapper.highlighted .md-toc-link{font-weight:800}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .markdown-preview{left:300px;width:calc(100% - 300px);padding:2em calc(50% - 457px - 300px / 2);margin:0;box-sizing:border-box}@media screen and (max-width:1274px){html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .markdown-preview{padding:2em}}@media screen and (max-width:450px){html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .markdown-preview{width:100%}}html body[for=html-export]:not([data-presentation-mode]):not([html-show-sidebar-toc]) .markdown-preview{left:50%;transform:translateX(-50%)}html body[for=html-export]:not([data-presentation-mode]):not([html-show-sidebar-toc]) .md-sidebar-toc{display:none}
/* Please visit the URL below for more information: */
/*   https://shd101wyy.github.io/markdown-preview-enhanced/#/customize-css */

      </style>
      <!-- The content below will be included at the end of the <head> element. --><script type="text/javascript">
  document.addEventListener("DOMContentLoaded", function () {
    // your code here
  });
</script></head><body for="html-export">
    
    
      <div class="crossnote markdown-preview  ">
      
<h1 id="nginx-安装与多端口配置指南">Nginx 安装与多端口配置指南 </h1>
<h2 id="一-简介">一、简介 </h2>
<p>Nginx 是一款高性能的 HTTP 和反向代理服务器，广泛应用于 Web 服务和负载均衡。本文将介绍在 Ubuntu 系统上安装 Nginx，并配置多个端口以支持多个前端页面服务。同时，我们将介绍如何为前端服务配置 API 请求的代理规则，包括不同路径和直接通过 IP 地址访问的 API。</p>
<h2 id="二-安装-nginx">二、安装 Nginx </h2>
<h3 id="1-更新系统包">1. 更新系统包 </h3>
<p>在安装任何软件之前，建议先更新系统包列表：</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash bash"><code><span class="token function">sudo</span> <span class="token function">apt</span> update
</code></pre><h3 id="2-安装-nginx">2. 安装 Nginx </h3>
<p>使用 Ubuntu 的默认包管理器安装 Nginx：</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash bash"><code><span class="token function">sudo</span> <span class="token function">apt</span> <span class="token function">install</span> nginx
</code></pre><p>设置系统开机自动启动Nginx</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash bash"><code>systemctl <span class="token builtin class-name">enable</span> nginx
</code></pre><h3 id="3-验证安装">3. 验证安装 </h3>
<p>安装完成后，可以通过以下命令检查 Nginx 是否正在运行：</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash bash"><code><span class="token function">sudo</span> systemctl status nginx
</code></pre><p>如果看到 <code>active (running)</code>，则表示 Nginx 已成功启动。</p>
<h3 id="4-测试默认页面">4. 测试默认页面 </h3>
<p>打开浏览器，访问服务器的 IP 地址或域名（例如 <code>http://your_server_ip</code>）。如果看到 Nginx 的默认欢迎页面，则说明安装成功。</p>
<h2 id="三-配置多端口服务">三、配置多端口服务 </h2>
<h3 id="1-理解-nginx-配置文件结构">1. 理解 Nginx 配置文件结构 </h3>
<p>Nginx 的主配置文件通常位于 <code>/etc/nginx/nginx.conf</code>。此外，Nginx 会加载 <code>/etc/nginx/sites-available/</code> 目录下的配置文件，这些文件通过符号链接连接到 <code>/etc/nginx/sites-enabled/</code> 目录。</p>
<p>为了方便管理多个端口的服务，建议为每个服务创建单独的配置文件，并将其放置在 <code>/etc/nginx/sites-available/</code> 目录中。</p>
<h3 id="2-创建多端口配置文件">2. 创建多端口配置文件 </h3>
<p>假设你有多个前端页面，分别运行在不同的端口上，例如 8080、8081 和 8082。以下是为每个服务创建配置文件的步骤：</p>
<h4 id="1-创建第一个服务配置文件">(1) 创建第一个服务配置文件 </h4>
<pre data-role="codeBlock" data-info="bash" class="language-bash bash"><code><span class="token function">sudo</span> <span class="token function">nano</span> /etc/nginx/sites-available/service1
</code></pre><p>在打开的编辑器中输入以下内容：</p>
<pre data-role="codeBlock" data-info="nginx" class="language-nginx nginx"><code><span class="token directive"><span class="token keyword keyword-server">server</span></span> <span class="token punctuation">{</span>
    <span class="token directive"><span class="token keyword keyword-listen">listen</span> <span class="token number">8080</span></span><span class="token punctuation">;</span>
    <span class="token directive"><span class="token keyword keyword-server_name">server_name</span> your_domain_or_ip</span><span class="token punctuation">;</span>

    <span class="token directive"><span class="token keyword keyword-location">location</span> /</span> <span class="token punctuation">{</span>
        <span class="token directive"><span class="token keyword keyword-root">root</span> /path/to/your/service1/files</span><span class="token punctuation">;</span>  <span class="token comment"># 替换为服务1的文件路径</span>
        <span class="token directive"><span class="token keyword keyword-index">index</span> index.html</span><span class="token punctuation">;</span>
        <span class="token comment"># 如果请求的文件不存在，直接返回首页</span>
        <span class="token directive"><span class="token keyword keyword-try_files">try_files</span> <span class="token variable">$uri</span> <span class="token variable">$uri</span>/ /index.html =404</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment"># 设置文件上传大小限制</span>
    <span class="token directive"><span class="token keyword keyword-client_max_body_size">client_max_body_size</span> <span class="token number">100M</span></span><span class="token punctuation">;</span>

    <span class="token comment"># 配置 API 请求代理</span>
    <span class="token directive"><span class="token keyword keyword-location">location</span> /prod-api/</span> <span class="token punctuation">{</span>
        <span class="token directive"><span class="token keyword keyword-proxy_pass">proxy_pass</span> http://192.168.10.131:10002/</span><span class="token punctuation">;</span>  <span class="token comment"># 替换为实际的 API 服务器地址</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> Host <span class="token variable">$host</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Real-IP <span class="token variable">$remote_addr</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Forwarded-For <span class="token variable">$proxy_add_x_forwarded_for</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Forwarded-Proto <span class="token variable">$scheme</span></span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token directive"><span class="token keyword keyword-location">location</span> /prod</span> <span class="token punctuation">{</span>
        <span class="token directive"><span class="token keyword keyword-proxy_pass">proxy_pass</span> http://api_server_ip_or_domain/prod</span><span class="token punctuation">;</span>  <span class="token comment"># 替换为实际的 API 服务器地址</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> Host <span class="token variable">$host</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Real-IP <span class="token variable">$remote_addr</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Forwarded-For <span class="token variable">$proxy_add_x_forwarded_for</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Forwarded-Proto <span class="token variable">$scheme</span></span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token directive"><span class="token keyword keyword-location">location</span> /direct-ip</span> <span class="token punctuation">{</span>
        <span class="token directive"><span class="token keyword keyword-proxy_pass">proxy_pass</span> http://api_server_ip/prod-api</span><span class="token punctuation">;</span>  <span class="token comment"># 替换为实际的 API 服务器 IP 地址</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> Host <span class="token variable">$host</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Real-IP <span class="token variable">$remote_addr</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Forwarded-For <span class="token variable">$proxy_add_x_forwarded_for</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Forwarded-Proto <span class="token variable">$scheme</span></span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment"># 错误页面处理</span>
    <span class="token directive"><span class="token keyword keyword-error_page">error_page</span> <span class="token number">404</span> /404.html</span><span class="token punctuation">;</span>  <span class="token comment"># 自定义 404 页面</span>
    <span class="token directive"><span class="token keyword keyword-error_page">error_page</span> <span class="token number">500</span> <span class="token number">502</span> <span class="token number">503</span> <span class="token number">504</span> /50x.html</span><span class="token punctuation">;</span>  <span class="token comment"># 自定义 500 系列错误页面</span>

    <span class="token comment"># 如果页面未找到，直接返回首页</span>
    <span class="token directive"><span class="token keyword keyword-location">location</span> = /404.html</span> <span class="token punctuation">{</span>
        <span class="token directive"><span class="token keyword keyword-internal">internal</span></span><span class="token punctuation">;</span>  <span class="token comment"># 防止直接访问</span>
        <span class="token directive"><span class="token keyword keyword-root">root</span> /path/to/your/service1/files</span><span class="token punctuation">;</span>  <span class="token comment"># 替换为自定义 404 页面的路径</span>
    <span class="token punctuation">}</span>

    <span class="token directive"><span class="token keyword keyword-location">location</span> = /50x.html</span> <span class="token punctuation">{</span>
        <span class="token directive"><span class="token keyword keyword-internal">internal</span></span><span class="token punctuation">;</span>  <span class="token comment"># 防止直接访问</span>
        <span class="token directive"><span class="token keyword keyword-root">root</span> /path/to/your/service1/files</span><span class="token punctuation">;</span>  <span class="token comment"># 替换为自定义 50x 页面的路径</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre><h4 id="2-创建第二个服务配置文件">(2) 创建第二个服务配置文件 </h4>
<pre data-role="codeBlock" data-info="bash" class="language-bash bash"><code><span class="token function">sudo</span> <span class="token function">nano</span> /etc/nginx/sites-available/service2
</code></pre><p>输入以下内容：</p>
<pre data-role="codeBlock" data-info="nginx" class="language-nginx nginx"><code><span class="token directive"><span class="token keyword keyword-server">server</span></span> <span class="token punctuation">{</span>
    <span class="token directive"><span class="token keyword keyword-listen">listen</span> <span class="token number">8081</span></span><span class="token punctuation">;</span>
    <span class="token directive"><span class="token keyword keyword-server_name">server_name</span> your_domain_or_ip</span><span class="token punctuation">;</span>

    <span class="token directive"><span class="token keyword keyword-location">location</span> /</span> <span class="token punctuation">{</span>
        <span class="token directive"><span class="token keyword keyword-root">root</span> /path/to/your/service2/files</span><span class="token punctuation">;</span>  <span class="token comment"># 替换为服务2的文件路径</span>
        <span class="token directive"><span class="token keyword keyword-index">index</span> index.html</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment"># 设置文件上传大小限制</span>
    <span class="token directive"><span class="token keyword keyword-client_max_body_size">client_max_body_size</span> <span class="token number">100M</span></span><span class="token punctuation">;</span>

    <span class="token comment"># 配置 API 请求代理</span>
    <span class="token directive"><span class="token keyword keyword-location">location</span> /prod-api</span> <span class="token punctuation">{</span>
        <span class="token directive"><span class="token keyword keyword-proxy_pass">proxy_pass</span> http://api_server_ip_or_domain/prod-api</span><span class="token punctuation">;</span>  <span class="token comment"># 替换为实际的 API 服务器地址</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> Host <span class="token variable">$host</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Real-IP <span class="token variable">$remote_addr</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Forwarded-For <span class="token variable">$proxy_add_x_forwarded_for</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Forwarded-Proto <span class="token variable">$scheme</span></span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token directive"><span class="token keyword keyword-location">location</span> /prod</span> <span class="token punctuation">{</span>
        <span class="token directive"><span class="token keyword keyword-proxy_pass">proxy_pass</span> http://api_server_ip_or_domain/prod</span><span class="token punctuation">;</span>  <span class="token comment"># 替换为实际的 API 服务器地址</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> Host <span class="token variable">$host</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Real-IP <span class="token variable">$remote_addr</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Forwarded-For <span class="token variable">$proxy_add_x_forwarded_for</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Forwarded-Proto <span class="token variable">$scheme</span></span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token directive"><span class="token keyword keyword-location">location</span> /direct-ip</span> <span class="token punctuation">{</span>
        <span class="token directive"><span class="token keyword keyword-proxy_pass">proxy_pass</span> http://api_server_ip/prod-api</span><span class="token punctuation">;</span>  <span class="token comment"># 替换为实际的 API 服务器 IP 地址</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> Host <span class="token variable">$host</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Real-IP <span class="token variable">$remote_addr</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Forwarded-For <span class="token variable">$proxy_add_x_forwarded_for</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Forwarded-Proto <span class="token variable">$scheme</span></span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre><h4 id="3-创建第三个服务配置文件">(3) 创建第三个服务配置文件 </h4>
<pre data-role="codeBlock" data-info="bash" class="language-bash bash"><code><span class="token function">sudo</span> <span class="token function">nano</span> /etc/nginx/sites-available/service3
</code></pre><p>输入以下内容：</p>
<pre data-role="codeBlock" data-info="nginx" class="language-nginx nginx"><code><span class="token directive"><span class="token keyword keyword-server">server</span></span> <span class="token punctuation">{</span>
    <span class="token directive"><span class="token keyword keyword-listen">listen</span> <span class="token number">8082</span></span><span class="token punctuation">;</span>
    <span class="token directive"><span class="token keyword keyword-server_name">server_name</span> your_domain_or_ip</span><span class="token punctuation">;</span>

    <span class="token directive"><span class="token keyword keyword-location">location</span> /</span> <span class="token punctuation">{</span>
        <span class="token directive"><span class="token keyword keyword-root">root</span> /path/to/your/service3/files</span><span class="token punctuation">;</span>  <span class="token comment"># 替换为服务3的文件路径</span>
        <span class="token directive"><span class="token keyword keyword-index">index</span> index.html</span><span class="token punctuation">;</span>
        <span class="token comment"># 如果请求的文件不存在，直接返回首页</span>
        <span class="token directive"><span class="token keyword keyword-try_files">try_files</span> <span class="token variable">$uri</span> <span class="token variable">$uri</span>/ /index.html =404</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment"># 设置文件上传大小限制</span>
    <span class="token directive"><span class="token keyword keyword-client_max_body_size">client_max_body_size</span> <span class="token number">100M</span></span><span class="token punctuation">;</span>

    <span class="token comment"># 配置 API 请求代理</span>
    <span class="token directive"><span class="token keyword keyword-location">location</span> /prod-api</span> <span class="token punctuation">{</span>
        <span class="token directive"><span class="token keyword keyword-proxy_pass">proxy_pass</span> http://api_server_ip_or_domain/prod-api</span><span class="token punctuation">;</span>  <span class="token comment"># 替换为实际的 API 服务器地址</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> Host <span class="token variable">$host</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Real-IP <span class="token variable">$remote_addr</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Forwarded-For <span class="token variable">$proxy_add_x_forwarded_for</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Forwarded-Proto <span class="token variable">$scheme</span></span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token directive"><span class="token keyword keyword-location">location</span> /prod</span> <span class="token punctuation">{</span>
        <span class="token directive"><span class="token keyword keyword-proxy_pass">proxy_pass</span> http://api_server_ip_or_domain/prod</span><span class="token punctuation">;</span>  <span class="token comment"># 替换为实际的 API 服务器地址</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> Host <span class="token variable">$host</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Real-IP <span class="token variable">$remote_addr</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Forwarded-For <span class="token variable">$proxy_add_x_forwarded_for</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Forwarded-Proto <span class="token variable">$scheme</span></span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token directive"><span class="token keyword keyword-location">location</span> /direct-ip</span> <span class="token punctuation">{</span>
        <span class="token directive"><span class="token keyword keyword-proxy_pass">proxy_pass</span> http://api_server_ip/prod-api</span><span class="token punctuation">;</span>  <span class="token comment"># 替换为实际的 API 服务器 IP 地址</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> Host <span class="token variable">$host</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Real-IP <span class="token variable">$remote_addr</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Forwarded-For <span class="token variable">$proxy_add_x_forwarded_for</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Forwarded-Proto <span class="token variable">$scheme</span></span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment"># 错误页面处理</span>
    <span class="token directive"><span class="token keyword keyword-error_page">error_page</span> <span class="token number">404</span> /404.html</span><span class="token punctuation">;</span>  <span class="token comment"># 自定义 404 页面</span>
    <span class="token directive"><span class="token keyword keyword-error_page">error_page</span> <span class="token number">500</span> <span class="token number">502</span> <span class="token number">503</span> <span class="token number">504</span> /50x.html</span><span class="token punctuation">;</span>  <span class="token comment"># 自定义 500 系列错误页面</span>

    <span class="token comment"># 如果页面未找到，直接返回首页</span>
    <span class="token directive"><span class="token keyword keyword-location">location</span> = /404.html</span> <span class="token punctuation">{</span>
        <span class="token directive"><span class="token keyword keyword-internal">internal</span></span><span class="token punctuation">;</span>  <span class="token comment"># 防止直接访问</span>
        <span class="token directive"><span class="token keyword keyword-root">root</span> /path/to/your/service1/files</span><span class="token punctuation">;</span>  <span class="token comment"># 替换为自定义 404 页面的路径</span>
    <span class="token punctuation">}</span>

    <span class="token directive"><span class="token keyword keyword-location">location</span> = /50x.html</span> <span class="token punctuation">{</span>
        <span class="token directive"><span class="token keyword keyword-internal">internal</span></span><span class="token punctuation">;</span>  <span class="token comment"># 防止直接访问</span>
        <span class="token directive"><span class="token keyword keyword-root">root</span> /path/to/your/service1/files</span><span class="token punctuation">;</span>  <span class="token comment"># 替换为自定义 50x 页面的路径</span>
    <span class="token punctuation">}</span>

<span class="token punctuation">}</span>
</code></pre><h3 id="3-简化配置文件启用方式">3. 简化配置文件启用方式 </h3>
<p>为了简化启用配置文件的步骤，可以编写一个简单的脚本来自动完成链接操作。</p>
<h4 id="1-创建脚本">(1) 创建脚本 </h4>
<p>创建一个脚本文件 <code>enable-site.sh</code>：</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash bash"><code><span class="token function">sudo</span> <span class="token function">nano</span> /usr/local/bin/enable-site.sh
</code></pre><p>在脚本中输入以下内容：</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash bash"><code><span class="token shebang important">#!/bin/bash</span>

<span class="token comment"># 检查是否提供了配置文件名</span>
<span class="token keyword keyword-if">if</span> <span class="token punctuation">[</span> <span class="token parameter variable">-z</span> <span class="token string">"<span class="token variable">$1</span>"</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword keyword-then">then</span>
    <span class="token builtin class-name">echo</span> <span class="token string">"Usage: <span class="token variable">$0</span> &lt;config-file-name&gt;"</span>
    <span class="token builtin class-name">exit</span> <span class="token number">1</span>
<span class="token keyword keyword-fi">fi</span>

<span class="token comment"># 创建符号链接</span>
<span class="token function">sudo</span> <span class="token function">ln</span> <span class="token parameter variable">-s</span> /etc/nginx/sites-available/<span class="token variable">$1</span> /etc/nginx/sites-enabled/

<span class="token comment"># 测试配置文件</span>
<span class="token function">sudo</span> nginx <span class="token parameter variable">-t</span>

<span class="token comment"># 如果测试通过，则重新加载 Nginx</span>
<span class="token keyword keyword-if">if</span> <span class="token punctuation">[</span> <span class="token variable">$?</span> <span class="token parameter variable">-eq</span> <span class="token number">0</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword keyword-then">then</span>
    <span class="token function">sudo</span> systemctl reload nginx
    <span class="token builtin class-name">echo</span> <span class="token string">"Site <span class="token variable">$1</span> enabled successfully."</span>
<span class="token keyword keyword-else">else</span>
    <span class="token builtin class-name">echo</span> <span class="token string">"Failed to enable site <span class="token variable">$1</span>. Please check the configuration file."</span>
<span class="token keyword keyword-fi">fi</span>
</code></pre><h4 id="2-赋予脚本执行权限">(2) 赋予脚本执行权限 </h4>
<pre data-role="codeBlock" data-info="bash" class="language-bash bash"><code><span class="token function">sudo</span> <span class="token function">chmod</span> +x /usr/local/bin/enable-site.sh
</code></pre><h4 id="3-使用脚本启用配置文件">(3) 使用脚本启用配置文件 </h4>
<p>现在，你可以通过以下命令启用配置文件，而无需手动创建符号链接：</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash bash"><code><span class="token function">sudo</span> enable-site.sh service1
<span class="token function">sudo</span> enable-site.sh service2
<span class="token function">sudo</span> enable-site.sh service3
</code></pre><h3 id="4-测试配置文件">4. 测试配置文件 </h3>
<p>在重新加载 Nginx 之前，建议先测试配置文件的语法是否正确：</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash bash"><code><span class="token function">sudo</span> nginx <span class="token parameter variable">-t</span>
</code></pre><p>如果没有错误，会显示 <code>syntax is ok</code> 和 <code>test is successful</code>。</p>
<h3 id="5-重新加载-nginx">5. 重新加载 Nginx </h3>
<p>配置文件测试通过后，重新加载 Nginx 以应用新的配置：</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash bash"><code><span class="token function">sudo</span> systemctl reload nginx
</code></pre><h3 id="6-访问服务">6. 访问服务 </h3>
<p>现在，你可以通过访问以下地址来测试每个服务：</p>
<ul>
<li><code>http://your_domain_or_ip:8080</code> 访问服务1</li>
<li><code>http://your_domain_or_ip:8081</code> 访问服务2</li>
<li><code>http://your_domain_or_ip:8082</code> 访问服务3</li>
</ul>
<h2 id="四-管理-nginx-服务">四、管理 Nginx 服务 </h2>
<h3 id="1-启动-停止和重启-nginx">1. 启动、停止和重启 Nginx </h3>
<ul>
<li>启动 Nginx：<pre data-role="codeBlock" data-info="bash" class="language-bash bash"><code><span class="token function">sudo</span> systemctl start nginx
</code></pre></li>
<li>停止 Nginx：<pre data-role="codeBlock" data-info="bash" class="language-bash bash"><code><span class="token function">sudo</span> systemctl stop nginx
</code></pre></li>
<li>重启 Nginx：<pre data-role="codeBlock" data-info="bash" class="language-bash bash"><code><span class="token function">sudo</span> systemctl restart nginx
</code></pre></li>
</ul>
<h3 id="2-查看-nginx-日志">2. 查看 Nginx 日志 </h3>
<p>Nginx 的日志文件通常位于 <code>/var/log/nginx/</code> 目录下。可以通过以下命令查看日志：</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash bash"><code><span class="token function">sudo</span> <span class="token function">tail</span> <span class="token parameter variable">-f</span> /var/log/nginx/error.log
<span class="token function">sudo</span> <span class="token function">tail</span> <span class="token parameter variable">-f</span> /var/log/nginx/access.log
</code></pre>
<h2 id="四-centos-7-特别说明">四、CentOS 7 特别说明</h2>
<h3 id="1-目录结构差异">1. 目录结构差异</h3>
<p>CentOS 7 的 Nginx 默认配置目录结构与 Ubuntu 不同：</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash bash"><code>/etc/nginx/
├── nginx.conf          # 主配置文件
├── conf.d/             # 存放服务配置文件
│   ├── default.conf    # 默认配置文件
│   └── your_site.conf  # 自定义配置文件
├── modules/            # 模块目录
└── ...
</code></pre>
<h3 id="2-修改启用脚本">2. 修改启用脚本</h3>
<p>针对 CentOS 7 修改 <code>enable-site.sh</code> 脚本：</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash bash"><code><span class="token function">sudo</span> <span class="token function">nano</span> /usr/local/bin/enable-site.sh
</code></pre>
<p>内容修改为：</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash bash"><code><span class="token shebang important">#!/bin/bash</span>

<span class="token comment"># 检查是否提供了配置文件名</span>
<span class="token keyword keyword-if">if</span> <span class="token punctuation">[</span> <span class="token parameter variable">-z</span> <span class="token string">"<span class="token variable">$1</span>"</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword keyword-then">then</span>
    <span class="token builtin class-name">echo</span> <span class="token string">"Usage: <span class="token variable">$0</span> &lt;config-file-name&gt;"</span>
    <span class="token builtin class-name">exit</span> <span class="token number">1</span>
<span class="token keyword keyword-fi">fi</span>

<span class="token comment"># CentOS 7 的配置目录</span>
<span class="token assign-left variable">CONF_DIR</span><span class="token operator">=</span><span class="token string">"/etc/nginx/conf.d"</span>

<span class="token comment"># 检查配置文件是否存在</span>
<span class="token keyword keyword-if">if</span> <span class="token punctuation">[</span> <span class="token operator">!</span> <span class="token parameter variable">-f</span> <span class="token string">"<span class="token variable">$CONF_DIR</span>/<span class="token variable">$1</span>"</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword keyword-then">then</span>
    <span class="token builtin class-name">echo</span> <span class="token string">"Error: Configuration file <span class="token variable">$CONF_DIR</span>/<span class="token variable">$1</span> does not exist!"</span>
    <span class="token builtin class-name">exit</span> <span class="token number">1</span>
<span class="token keyword keyword-fi">fi</span>

<span class="token comment"># 测试配置文件</span>
<span class="token function">sudo</span> nginx <span class="token parameter variable">-t</span>

<span class="token comment"># 如果测试通过，则重新加载 Nginx</span>
<span class="token keyword keyword-if">if</span> <span class="token punctuation">[</span> <span class="token variable">$?</span> <span class="token parameter variable">-eq</span> <span class="token number">0</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword keyword-then">then</span>
    <span class="token function">sudo</span> systemctl reload nginx
    <span class="token builtin class-name">echo</span> <span class="token string">"Site <span class="token variable">$1</span> enabled successfully."</span>
<span class="token keyword keyword-else">else</span>
    <span class="token builtin class-name">echo</span> <span class="token string">"Failed to enable site <span class="token variable">$1</span>. Please check the configuration file."</span>
    <span class="token builtin class-name">exit</span> <span class="token number">1</span>
<span class="token keyword keyword-fi">fi</span>
</code></pre>
<h3 id="3-centos-7-上的操作步骤">3. CentOS 7 上的操作步骤</h3>
<ol>
<li><p>安装 Nginx（使用 yum 而不是 apt）：</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash bash"><code><span class="token function">sudo</span> <span class="token function">yum</span> <span class="token function">install</span> nginx
</code></pre></li>
<li><p>创建配置文件（放在 <code>/etc/nginx/conf.d/</code> 目录）：</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash bash"><code><span class="token function">sudo</span> <span class="token function">nano</span> /etc/nginx/conf.d/service1.conf
</code></pre></li>
<li><p>赋予脚本执行权限：</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash bash"><code><span class="token function">sudo</span> <span class="token function">chmod</span> +x /usr/local/bin/enable-site.sh
</code></pre></li>
<li><p>使用脚本启用配置（注意需要包含 <code>.conf</code> 后缀）：</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash bash"><code><span class="token function">sudo</span> enable-site.sh service1.conf
</code></pre></li>
<li><p>处理 SELinux 和防火墙：</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash bash"><code><span class="token comment"># 临时禁用SELinux（如需）</span>
<span class="token function">sudo</span> setenforce <span class="token number">0</span>

<span class="token comment"># 或修改安全上下文</span>
<span class="token function">sudo</span> chcon -Rt httpd_sys_content_t /path/to/your/files

<span class="token comment"># 开放防火墙端口</span>
<span class="token function">sudo</span> firewall-cmd --permanent --add-port=8080/tcp
<span class="token function">sudo</span> firewall-cmd --reload
</code></pre></li>
</ol>
<h2 id="五-额外配置建议">五、额外配置建议 </h2>
<h3 id="1-配置-ssltls">1. 配置 SSL/TLS </h3>
<p>如果需要为服务启用 HTTPS，可以使用 Let's Encrypt 提供的免费 SSL 证书。安装 Certbot 并为你的服务配置 SSL：</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash bash"><code><span class="token function">sudo</span> <span class="token function">apt</span> <span class="token function">install</span> certbot python3-certbot-nginx
<span class="token function">sudo</span> certbot <span class="token parameter variable">--nginx</span> <span class="token parameter variable">-d</span> your_domain_or_ip
</code></pre><h3 id="2-配置反向代理">2. 配置反向代理 </h3>
<p>如果前端页面是通过其他服务（如 Node.js 或 Flask）运行的，可以将 Nginx 配置为反向代理。例如：</p>
<pre data-role="codeBlock" data-info="nginx" class="language-nginx nginx"><code><span class="token directive"><span class="token keyword keyword-server">server</span></span> <span class="token punctuation">{</span>
    <span class="token directive"><span class="token keyword keyword-listen">listen</span> <span class="token number">8080</span></span><span class="token punctuation">;</span>
    <span class="token directive"><span class="token keyword keyword-server_name">server_name</span> your_domain_or_ip</span><span class="token punctuation">;</span>

    <span class="token directive"><span class="token keyword keyword-location">location</span> /</span> <span class="token punctuation">{</span>
        <span class="token directive"><span class="token keyword keyword-proxy_pass">proxy_pass</span> http://localhost:3000</span><span class="token punctuation">;</span>  <span class="token comment"># 替换为目标服务的地址</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> Host <span class="token variable">$host</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Real-IP <span class="token variable">$remote_addr</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Forwarded-For <span class="token variable">$proxy_add_x_forwarded_for</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Forwarded-Proto <span class="token variable">$scheme</span></span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment"># 设置文件上传大小限制</span>
    <span class="token directive"><span class="token keyword keyword-client_max_body_size">client_max_body_size</span> <span class="token number">100M</span></span><span class="token punctuation">;</span>

    <span class="token comment"># 配置 API 请求代理</span>
    <span class="token directive"><span class="token keyword keyword-location">location</span> /prod-api</span> <span class="token punctuation">{</span>
        <span class="token directive"><span class="token keyword keyword-proxy_pass">proxy_pass</span> http://api_server_ip_or_domain/prod-api</span><span class="token punctuation">;</span>  <span class="token comment"># 替换为实际的 API 服务器地址</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> Host <span class="token variable">$host</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Real-IP <span class="token variable">$remote_addr</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Forwarded-For <span class="token variable">$proxy_add_x_forwarded_for</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Forwarded-Proto <span class="token variable">$scheme</span></span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token directive"><span class="token keyword keyword-location">location</span> /prod</span> <span class="token punctuation">{</span>
        <span class="token directive"><span class="token keyword keyword-proxy_pass">proxy_pass</span> http://api_server_ip_or_domain/prod</span><span class="token punctuation">;</span>  <span class="token comment"># 替换为实际的 API 服务器地址</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> Host <span class="token variable">$host</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Real-IP <span class="token variable">$remote_addr</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Forwarded-For <span class="token variable">$proxy_add_x_forwarded_for</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Forwarded-Proto <span class="token variable">$scheme</span></span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token directive"><span class="token keyword keyword-location">location</span> /direct-ip</span> <span class="token punctuation">{</span>
        <span class="token directive"><span class="token keyword keyword-proxy_pass">proxy_pass</span> http://api_server_ip/prod-api</span><span class="token punctuation">;</span>  <span class="token comment"># 替换为实际的 API 服务器 IP 地址</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> Host <span class="token variable">$host</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Real-IP <span class="token variable">$remote_addr</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Forwarded-For <span class="token variable">$proxy_add_x_forwarded_for</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Forwarded-Proto <span class="token variable">$scheme</span></span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre><h3 id="3-配置文件的集中管理">3. 配置文件的集中管理 </h3>
<p>如果你有多个服务配置文件，可以将它们统一管理在一个文件中，然后通过 <code>include</code> 指令加载。例如，创建一个 <code>services.conf</code> 文件：</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash bash"><code><span class="token function">sudo</span> <span class="token function">nano</span> /etc/nginx/conf.d/services.conf
</code></pre><p>在文件中添加：</p>
<pre data-role="codeBlock" data-info="nginx" class="language-nginx nginx"><code><span class="token directive"><span class="token keyword keyword-include">include</span> /etc/nginx/sites-available/*.conf</span><span class="token punctuation">;</span>
</code></pre><p>然后将所有服务配置文件放在 <code>/etc/nginx/sites-available/</code> 目录下，Nginx 会自动加载这些文件。</p>
<h3 id="4api代理">4.API代理 </h3>
<ol>
<li>无额外头信息透传，需要路径重写的场景</li>
</ol>
<pre data-role="codeBlock" data-info="nginx" class="language-nginx nginx"><code>     <span class="token directive"><span class="token keyword keyword-location">location</span>  /prod-api/</span> <span class="token punctuation">{</span>
            <span class="token directive"><span class="token keyword keyword-rewrite">rewrite</span> ^/prod-api/(.*)$ /<span class="token variable">$1</span> break</span><span class="token punctuation">;</span>
            <span class="token directive"><span class="token keyword keyword-proxy_pass">proxy_pass</span> http://183.249.68.194:14031</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> 
</code></pre><ol start="2">
<li>透传客户端 IP、协议等关键头信息,标准 API 代理，需保留路径和头信息完整性</li>
</ol>
<pre data-role="codeBlock" data-info="nginx" class="language-nginx nginx"><code>     <span class="token directive"><span class="token keyword keyword-location">location</span> /prod-api/</span> <span class="token punctuation">{</span>
    <span class="token directive"><span class="token keyword keyword-proxy_pass">proxy_pass</span> http://192.168.10.131:10002/</span><span class="token punctuation">;</span>  <span class="token comment"># 注意末尾的 `/` 确保路径拼接正确</span>
    <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> Host <span class="token variable">$host</span></span><span class="token punctuation">;</span>
    <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Real-IP <span class="token variable">$remote_addr</span></span><span class="token punctuation">;</span>
    <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Forwarded-For <span class="token variable">$proxy_add_x_forwarded_for</span></span><span class="token punctuation">;</span>
    <span class="token directive"><span class="token keyword keyword-proxy_set_header">proxy_set_header</span> X-Forwarded-Proto <span class="token variable">$scheme</span></span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre><p>关键注意事项<br>
​<strong>proxy_pass 末尾的 /</strong><br>
如果后端接口本身无 /prod-api 前缀，需在 proxy_pass 末尾加 /：</p>
<pre data-role="codeBlock" data-info="nginx" class="language-nginx nginx"><code><span class="token directive"><span class="token keyword keyword-proxy_pass">proxy_pass</span> http://192.168.10.131:10002/</span><span class="token punctuation">;</span>  <span class="token comment"># 请求 /prod-api/user → 代理到 http://IP:10002/user</span>
如果后端需要保留 /prod-api 前缀，则去掉 /：
</code></pre><pre data-role="codeBlock" data-info="nginx" class="language-nginx nginx"><code><span class="token directive"><span class="token keyword keyword-proxy_pass">proxy_pass</span> http://192.168.10.131:10002</span><span class="token punctuation">;</span>  <span class="token comment"># 请求 /prod-api/user → 代理到 http://IP:10002/prod-api/user</span>
​前端与后端路径匹配
确保后端接口能处理 /prod-api/xxx 的路径（或通过 Nginx 重写适配）。
如果后端接口无前缀，建议在前端配置 VITE_API_BASE=/prod-api，Nginx 配置中加 / 重写：
</code></pre><pre data-role="codeBlock" data-info="nginx" class="language-nginx nginx"><code><span class="token directive"><span class="token keyword keyword-location">location</span> /prod-api/</span> <span class="token punctuation">{</span>
    <span class="token directive"><span class="token keyword keyword-proxy_pass">proxy_pass</span> http://backend-server/</span><span class="token punctuation">;</span>  <span class="token comment"># 末尾的 `/` 会移除 /prod-api 前缀</span>
    <span class="token comment"># 其他头设置...</span>
<span class="token punctuation">}</span>
</code></pre><h2 id="六-总结">六、总结 </h2>
<p>通过以上步骤，你可以在 Ubuntu 上安装 Nginx，并为其配置多个端口以支持多个前端页面服务。此外，我们还介绍了如何设置文件上传大小限制，并提供了更简便的方式来启用配置文件。最后，我们详细说明了如何为前端服务配置 API 请求的代理规则，包括不同路径（如 <code>/prod-api</code>、<code>/prod</code>）和直接通过 IP 地址访问的 API。这种配置方式便于管理和维护，同时也能灵活地调整每个服务的配置。</p>

      </div>
      
      
    
    
    
    
    
    
  
    </body></html>